package com.uenpay.im.ui.old.activity.attendance;

import com.qiyunxin.android.http.net.HttpRequestType;
import com.uenpay.im.IMApplication;
import com.uenpay.im.invokeitems.HttpRequestHandle;

import java.util.ArrayList;

/**
 * @author SL
 * @created 2016-3-16 下午7:03:55
 * @copyRight http://www.qiyunxin.com
 * @function 考勤打卡
 */
public class AttendanceHandle {

	public interface IAttendanceResultListener {
		void onResult(int status, String msg);
	}

	/**
	 * 签到签退
	 *
	 * @param entid
	 *            企业组织
	 * @param type
	 *            1签到 2签退
	 * @param longitude
	 *            经度
	 * @param latitude
	 *            纬度
	 */
	public void addAttendance(String entid, int type, String longitude,
			String latitude,
			final IAttendanceResultListener mIAttendanceResultListener) {
		AttendanceInvokeItem item = new AttendanceInvokeItem(entid, type,
				longitude, latitude);
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
										 String failReason) {
						if (resultObject != null) {
							IMApplication.getInstance().regDeviceNo();
							AttendanceInvokeItem.AttendanceInvokeItemResult result = ((AttendanceInvokeItem) resultObject)
									.getOutPut();
							if (result != null) {
								if (mIAttendanceResultListener != null) {
									mIAttendanceResultListener.onResult(
											result.status, result.msg);
								}
							} else {
								if (mIAttendanceResultListener != null) {
									mIAttendanceResultListener.onResult(-1,
											failReason);
								}
							}
						} else {
							if (mIAttendanceResultListener != null) {
								mIAttendanceResultListener.onResult(-1,
										failReason);
							}
						}
					}
				});

	}

	public interface IAttendanceListResultListener {
		void onAttendanceListResult(int status, String msg,
													ArrayList<AttendanceEntity> arrayList);
	}

	/**
	 * 获取统计列表
	 *
	 * @param entid
	 * @param custids
	 * @param startdate
	 * @param enddate
	 * @param page
	 * @param mIAttendanceListResultListener
	 */
	public void getAttendanceList(String entid, String custids,
			String startdate, String enddate, int page,
			final IAttendanceListResultListener mIAttendanceListResultListener) {
		AttendanceListInvokeItem item = new AttendanceListInvokeItem(entid,
				custids, startdate, enddate, page);
		HttpRequestHandle.onBaseRequest(item,
				HttpRequestType.SERVER_FAIL_AND_CACHE,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							IMApplication.getInstance().regDeviceNo();
							AttendanceListInvokeItem.AttendanceListInvokeItemResult result = ((AttendanceListInvokeItem) resultObject)
									.getOutPut();
							if (result != null) {

								mIAttendanceListResultListener
										.onAttendanceListResult(result.status,
												result.msg, result.arrayList);

							} else {
								if (mIAttendanceListResultListener != null) {
									mIAttendanceListResultListener
											.onAttendanceListResult(-1, "",
													null);
								}
							}
						} else {
							if (mIAttendanceListResultListener != null) {
								mIAttendanceListResultListener
										.onAttendanceListResult(-1, "", null);
							}
						}
					}
				});

	}

	/**
	 * 设置签到时间
	 *
	 * @param entid
	 * @param startTime
	 * @param endTime
	 * @param latitude
	 * @param longitude
	 * @param mIAttendanceResultListener
	 */
	public void setTimeLable(int entid, String startTime, String endTime,
			String latitude, String longitude,
			final IAttendanceResultListener mIAttendanceResultListener) {
		SetTimeTableInvokeItem item = new SetTimeTableInvokeItem(entid,
				startTime, endTime, latitude, longitude);
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							IMApplication.getInstance().regDeviceNo();
							SetTimeTableInvokeItem.SetTimeTableInvokeItemResult result = ((SetTimeTableInvokeItem) resultObject)
									.getOutPut();
							if (result != null) {
								mIAttendanceResultListener.onResult(
										result.status, result.msg);
							} else {
								if (mIAttendanceResultListener != null) {
									mIAttendanceResultListener.onResult(-1,
											failReason);
								}
							}
						} else {
							if (mIAttendanceResultListener != null) {
								mIAttendanceResultListener.onResult(-1,
										failReason);
							}
						}
					}
				});

	}

	public interface IGetSignInTimeListener {
		void onGetSignInResult(int status, String msg, String address,
										String sign_in_time, String sign_out_time, String latitude,
										String longitude);
	}

	/**
	 * 或签到时间
	 *
	 * @param entid
	 * @param mIGetSignInTimeListener
	 */
	public void getSignInTime(int entid,
			final IGetSignInTimeListener mIGetSignInTimeListener) {
		GetSignInTimeInvokeItem item = new GetSignInTimeInvokeItem(entid);
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							IMApplication.getInstance().regDeviceNo();
							GetSignInTimeInvokeItem.GetSignInTimeInvokeItemResult result = ((GetSignInTimeInvokeItem) resultObject)
									.getOutPut();
							if (result != null) {
								mIGetSignInTimeListener.onGetSignInResult(
										result.status, result.msg,
										result.address, result.sign_in_time,
										result.sing_out_time, result.latitude,
										result.longitude);

							} else {
								if (mIGetSignInTimeListener != null) {
									mIGetSignInTimeListener.onGetSignInResult(
											-1, "", "", "", "", "", "");
								}
							}
						} else {
							if (mIGetSignInTimeListener != null) {
								mIGetSignInTimeListener.onGetSignInResult(-1,
										failReason, "", "", "", "", "");
							}
						}
					}
				});

	}

}
